#################################################
#This performs the analyses reported in Table OB8
#################################################

###Log
my_log <- file("table_ob8_log.txt")

sink(my_log, append=T, type="output")
sink(my_log, append=T, type="message")

cat(readChar(rstudioapi::getSourceEditorContext()$path, # Writing currently opened R script to file
             file.info(rstudioapi::getSourceEditorContext()$path)$size))

start_time <- Sys.time()
start_time


library(rio)
library(socviz)
library(multcomp)
library(tidyverse)
library(broom)
library(margins)
library(ggstance)

###############################
# Analysis to test difference in variance
# in policy placement by condition
###############################

exp3 <- import("./Data/exp3_cleaned.csv") %>% 
  filter(pid_3 == "Democrat" | pid_3 == "Republican") %>%
  mutate(treat_3 = factor(treat_3, 
                          levels=c("No Party Cue", "Party Cue", "Party Cue w/ Group Pressure"), 
                          labels=c("No Party Cue", "Party Cue", "Cue w/Insinuation")), 
         treat_5 = factor(treat_5, 
                          levels=c("No Party Cue", "Party Cue", "Interest Groups", "Companies", "Labor Unions")), 
         change = factor(change, levels=c("Conservative Change", "Liberal Change")), 
         stereo = factor(change_stereotype,   levels=c("Non-Stereotypical Change", "Stereotypical Change"), 
                         labels=c("Counter-Stereotypical", "Stereotypical")), 
         pid_2 = factor(pid_2, levels=c("Republican", "Democrat"))) %>%
  dplyr::select(pid_2, treat_3, treat_5, change, stereo, ideol, pid_2, placement)


########################
# Standard Deviations
########################
exp3 %>% 
  group_by(treat_3) %>%
  summarize(sd = sd(placement, na.rm=T))

exp3 %>%
  group_by(treat_5) %>%
  summarize(sd = sd(placement, na.rm=T))


exp3 %>% 
  group_by(stereo, treat_3) %>%
  summarize(sd = sd(placement, na.rm=T))

exp3 %>%
  group_by(stereo, treat_5) %>%
  summarize(sd = sd(placement, na.rm=T))

########################
# Standard Deviations
########################

#Party Cue vs. Merged Insinuation (all, stereotypical, then counter-stereotypical)
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_3 !="No Party Cue"))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_3 !="No Party Cue" & stereo=="Stereotypical"))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_3 !="No Party Cue" & stereo=="Counter-Stereotypical"))

#Party Cue vs. Interest Groups
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Companies", "Labor Unions")))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Companies", "Labor Unions") & stereo == "Stereotypical"))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Companies", "Labor Unions") & stereo=="Counter-Stereotypical"))

#Party Cue Vs. Companies
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Interest Groups", "Labor Unions")))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Interest Groups", "Labor Unions") & stereo == "Stereotypical"))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Interest Groups", "Labor Unions") & stereo=="Counter-Stereotypical"))

#Party Cue vs. Unions
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Interest Groups", "Companies")))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Interest Groups", "Companies") & stereo == "Stereotypical"))
fligner.test(placement ~ treat_3, data=exp3, subset=(treat_5 != c("No Party Cue", "Interest Groups", "Companies") & stereo=="Counter-Stereotypical"))



end_time <- Sys.time()
end_time

closeAllConnections()